<?php
			ob_start();			
?>
<?php
		
		if (isset($_COOKIE["user"])) {
			// if cookie <> ""
		  }
		  else
		  {
			   header("location:login.php");
			  
		  }
?>
<!-- begin ckeidtor -->
<script type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({			 
	theme : "advanced",
	mode : "textareas",
    plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
	// Theme options
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,fullscreen,insertfile,insertimage",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : true,
	theme_advanced_font_sizes : "10px,12px,14px,16px,24px",
});
</script>
<!-- end cheditor-->

<?php
function updatePublish($reply)
{	
	$reply = $reply == 1 ? 0 : 1;
	$con_id_pub = $_REQUEST["con_id_pub"];
	$sql = "UPDATE tbl_contact SET reply={$reply} WHERE con_id=$con_id_pub";
	query($sql);	
}
?>
<?php
if(isset($_REQUEST["reply"]))
{
	updatePublish($_REQUEST["reply"]);
	
}
?>
<?php
function deleteRecord($id)
{
	
	$sql = "DELETE FROM  tbl_contact WHERE con_id={$con_id}";
	$result = query($sql);	
	if($result)
	{
		errorMessage("Supprimer réussies!");
	}	
	else
	{
		errorMessage("Supprimer échec ..!");
	}	
}
?>
<?php
	if(isset($_REQUEST["con_id"]))
{
	$con_id = $_REQUEST["con_id"];
	deleteRecord($id);
}
?>
<?php
	// when submit is click to reply mail
    if(isset($_POST["btnsubmit"]))
	{
		//get message to send
		$message = escapeValue($_POST["txtmessage"]);
		//set mail sender and subject of mail
		$from = "doorproject.fr";
		$subject = "doorproject.fr";
				
		if($message=="")
		{
			errorMessage("Message ne peut pas être .. vides!");
		}else
		{
				//select all email from DB
			$sql="SELECT * FROM tbl_contact WHERE reply=1";
			$result=query($sql);
			
			$i=0;
			$num=mysql_num_rows($result);
			
			while($row=mysql_fetch_array($result))
			{
				// set the mail will send to
				$to = $row["email"];
				
			   //end of message
				$headers  = "From: $from\r\n";
				$headers .= "Content-type: text/html\r\n";
			
				//options to send to cc+bcc
				//$headers .= "Cc: [email]maa@p-i-s.cXom[/email]";
				//$headers .= "Bcc: [email]email@maaking.cXom[/email]";
				
				// now lets send the email.
				$send_contact = mail($to, $subject, $message, $headers);

				if(!$send_contact){
					errorMessage("ERROR");
					
				}
				
				$i=$i+1;
				if($i==$num)
					{
						errorMessage("Répondre avec succès......!");
					}
				
				
			}// end while
			
		}
	}
?> 
                	<div id="border_cont_title">
                        	<div id='content_title_left'>gestion des contacts</div>
                            <div id='content_title_right'></div>
					</div> <!--end border_cont_title-->
                     <?php echo @$errors;?>
                    <div class="contact">
                        <form action="index.php?page=contact" method="post" name="frmcontact">
                            <table width="100%">
                                <tr>
                                    <td width="30%">Message de réponse:</td>
                                    <td width="70%"><textarea name="txtmessage" class="ckeditor" cols="40" rows="10" id="txtmessage" wrap="virtual"></textarea></td>
                                </tr>
                           
                                <tr>
                                    <td colspan="2">
                                         <input  style="float:right; height:35px; width:100px;" type="submit" name="btnsubmit" value="Répondre"  />
                                    </td>
                                </tr>
                            </table>
                        </form>
                    </div>
                    <br/>
                   
                    	<table width="770px">
                            	<tr>
                                    <th>Nom</th>
                                    <th>E-mail </th>
                                    <th>Téléphone</th>
                                    <th>Création</th>
                                    <th>Message</th>
                                    <th>Répondre</th>
                                    <th>Action</th>
                                </tr>
                             
                                <?php
								//include function from cls_pag
								include('../includes/cls_pagination.php');
								

                                    $sql = "SELECT * from  tbl_contact"; 
                                    $result = query($sql);
									$Num_Rows = mysql_num_rows($result);
							
									$Per_Page = 10;   // Per Page
									
									@$Page = $_GET["Page"];
									if(!@$Page)
									{
										$Page=1;
									}
									
									$Prev_Page = $Page-1;
									$Next_Page = $Page+1;
									
									$Page_Start = (($Per_Page*$Page)-$Per_Page);
									if($Num_Rows<=$Per_Page)
									{
										$Num_Pages =1;
									}
									else if(($Num_Rows % $Per_Page)==0)
									{
										$Num_Pages =($Num_Rows/$Per_Page) ;
									}
									else
									{
										$Num_Pages =($Num_Rows/$Per_Page)+1;
										$Num_Pages = (int)$Num_Pages;
									}
									
									$sql .=" ORDER BY con_id DESC  LIMIT $Page_Start , $Per_Page";
									$result  = mysql_query($sql);
                                    while($row = fetch_assoc($result))
                                    {
                                ?>
                                <tr>
                                	<td align="center" width="150px"><?php echo $row["name"];?></td>
                                    <td align="center" width="150px"><?php echo $row["email"];?></td>
                                    <td align="center" width="150px"><?php echo $row["telephone"];?></td>
                                    <td align="center" width="150px"><?php echo $row["pro_name"];?></td>
                                    <td align="left" width="250px">
										<?php 
											 $countSting=$row["message"];
											 if(strlen($countSting)<=20)
											 {
												  echo $sms=$countSting;
											 }
											 else
											 {
												 echo $sms=substr($countSting,0,20) ."...";
										?>
                                        			<a href="index.php?page=contact_detail&con_id=<?php echo $row["con_id"];?>" style="color:#F00;">
                                                        Lire
                                                    </a>
                                        <?php
											 }
										?>
                                        
                                    </td>
                                   <td align="center" width="50px">
                                   	<?php 
                                            $icon_publish = $row["reply"]==0 ? "publish_x.png" : "publish_y.png";
                                            $publish_image = "<img width='15px' src='images/{$icon_publish}' />";														
                                     ?>
                                            <a href="index.php?page=contact&reply=<?php echo $row["reply"] . "&con_id_pub=" . $row["con_id"];?>">
                                            <?php echo $publish_image; ?></a>
                                   </td>
                                    <td align="center">
                                   
                                   <a href="index.php?page=contact&con_id=<?php echo $row["con_id"];?>" onclick="return confirm('Êtes-vous sûr de vouloir supprimer ce contact?');"><img class="action_distant" src="images/delete.png" /></a>
                                    </td>
                                </tr>
                                <?php
									}//end while

                                ?>
                            </table>
                         <!--end table -->
                     <?php
								echo "<div id='Paginator_left'> Total $Num_Rows Record ";
								
                                
                                    $pages = new Paginator;
                                    $pages->items_total = $Num_Rows;
                                    $pages->mid_range = 10;
                                    $pages->current_page = $Page;
                                    $pages->default_ipp = $Per_Page;
                                  $pages->url_next = "index.php?page=contact&QueryString=value&Page=";
                                    $pages->paginate();
                                    
                                    echo $pages->display_pages();
									
									
                               echo " </div>" ;
							
                   ?>
                